package com.wangyi.fightimg.moduls;


import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.wangyi.fightimg.R;
import com.wangyi.fightimg.base.BaseFragmentActivity;
import com.wangyi.fightimg.moduls.category.CategoryFragment;
import com.wangyi.fightimg.moduls.main.MainFragment;
import com.wangyi.fightimg.utils.ShareUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;


public class MainActivity extends BaseFragmentActivity {

    @Bind(R.id.fl_moduls)
    FrameLayout flModuls;
    @Bind(R.id.rb_main)
    RadioButton rbMain;// 首页
    @Bind(R.id.rb_category)
    RadioButton rbCategory;// 分类
    @Bind(R.id.rb_search)
    RadioButton rbSearch;// 搜索
    @Bind(R.id.ll_root)
    LinearLayout llRoot;

    private int currentFragmentIndex;// 当前展示的Fragment下标
    private Fragment currentFragment;// 当前展示的Fragment
    public IUiListener iUiListener;// QQ分享监听回调

    @Override
    protected void addLayout() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void findView() {
        ButterKnife.bind(this);
        hideBar(true);
    }

    @Override
    protected void addListener() {
        rbMain.setOnClickListener(this);
        rbCategory.setOnClickListener(this);
        rbSearch.setOnClickListener(this);
    }

    @Override
    protected void initializedPage() {
        iUiListener = ShareUtil.initViews(context);
        currentFragmentIndex = 0;
        rbMain.setChecked(true);
        showFragment(currentFragmentIndex);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rb_main:// 首页
                if (currentFragmentIndex != 0) {
                    rbMain.setSelected(true);
                    currentFragmentIndex = 0;
                    showFragment(currentFragmentIndex);
                }
                break;
            case R.id.rb_category:// 分类
                if (currentFragmentIndex != 1) {
                    currentFragmentIndex = 1;
                    showFragment(currentFragmentIndex);

                }
                break;
            case R.id.rb_search:// 搜索
                Toast.makeText(context, "暂未开放", Toast.LENGTH_LONG).show();
//                if (currentFragmentIndex != 2) {
//                    currentFragmentIndex = 2;
//                    showFragment(currentFragmentIndex);
//                }
                break;
            default:
                break;
        }
    }

    /**
     * 展示制定的Fragment
     *
     * @param index
     */
    private void showFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        currentFragment = fragmentManager.findFragmentByTag("fragment" + index);
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            switch (index) {
                case 0:// 首页
                    currentFragment = new MainFragment();
                    break;
                case 1:// 分类
                    currentFragment = new CategoryFragment();
                    break;
                case 2:
                    break;
                default:
                    break;
            }
            transaction.add(R.id.fl_moduls, currentFragment, "fragment" + index);
        }
        transaction.commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // QQ分享回调
        if (requestCode == Constants.REQUEST_QQ_SHARE ||
                resultCode == Constants.ACTIVITY_OK) {
            Tencent.onActivityResultData(requestCode, resultCode, data, iUiListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private boolean isExit = false;

    @Override
    public void onBackPressed() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(context, "再按一次推出程序", Toast.LENGTH_SHORT).show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2500);
        } else {
            finish();
            System.exit(0);
        }

    }
}
